<template>
    <div class="discover">
        <div class="header">
            发现页
        </div>
        <div class="refresh">
            <v-refresh :on-refresh="onRefresh" :on-infinite="onInfinite" :noFlag="noFlag">
                <tag-picker v-model="tag"></tag-picker>
                <article-list :articleList="listData"></article-list>
            </v-refresh>
        </div>
        <navigation></navigation>
    </div>
</template>

<script>
    import navigation from '@/components/navigation';
    import vRefresh from '@/components/refresh';
    import tagPicker from '@/components/tagpicker';
    import articleList from '@/components/articlelist';

    export default {
        name: 'discover',
        data() {
            return {
                currentPage: 1, // 起始页
                itemsPerPage: 10, // 一页显示多少条
                pageStart: 0, // 开始页数
                pageEnd: 0, // 结束页数
                listData: [], // 数据存放数组
                noFlag: false,
                tag: undefined
            };
        },
        components: {
            navigation,
            vRefresh,
            tagPicker,
            articleList
        },
        created() {
        },
        mounted() {
            this.getList();
        },
        methods: {
            getList() {
                const more = this.$el.querySelector('.load-more');
                this.$Api.getArticleList({
                    currentPage: this.currentPage,
                    // eslint-disable-next-line
                    tagId: (this.tag && this.tag._id) || undefined
                }).then((res) => {
                    if (res.data.data.length === 10) {
                        this.listData = res.data.data;
                        more.style.display = 'block';
                        this.noFlag = false;
                    } else if (res.data.data.length === 0) {
                        this.listData = [];
                        more.style.display = 'none'; // 隐藏加载条
                        // 走完数据调用方法
                        this.noFlag = true;
                    } else if (res.data.data.length < 10 && res.data.data.length > 0) {
                        this.listData = res.data.data;
                        more.style.display = 'none'; // 隐藏加载条
                        // 走完数据调用方法
                        this.noFlag = true;
                    }
                }).catch((error) => {
                    console.log(error);
                });
            },
            onRefresh(done) {
                this.noFlag = false;
                this.currentPage = 1;
                this.getList();
                done(); // call done
            },
            onInfinite(done) {
                if (!this.noFlag) {
                    this.currentPage++;
                    const more = this.$el.querySelector('.load-more');
                    more.style.display = 'block';
                    this.noFlag = false;
                    this.$Api.getArticleList({
                        currentPage: this.currentPage,
                        // eslint-disable-next-line
                        tagId: (this.tag && this.tag._id) || undefined
                    }).then((res) => {
                        if (res.data.data.length === 0) {
                            more.style.display = 'none'; // 隐藏加载条
                            // 走完数据调用方法
                            this.noFlag = true;
                        } else if (res.data.data.length < 10 && res.data.data.length !== 0) {
                            this.listData = this.listData.concat(res.data.data);
                            more.style.display = 'none'; // 隐藏加载条
                            // 走完数据调用方法
                            this.noFlag = true;
                        } else if (res.data.count === 10) {
                            this.listData.concat(res.data.data);
                        }
                    }).catch((error) => {
                        console.log(error);
                    });
                    done();
                }
            }
        },
        watch: {
            tag(tagId) {
                if (tagId) {
                    this.currentPage = 1;
                    this.getList();
                }
            }
        }
    }
    ;
</script>

<style lang="scss">
    .discover {
        .header {
            top: 0px;
            height: 44px;
            width: 100%;
            background: rgb(253, 163, 62);
            line-height: 44px;
            font-size: 17px;
            position: fixed;
            z-index: 999;
            color: white;
            font-weight: 300;
            text-align: center;
        }
        .refresh {
            position: relative;
            height: 100vh;
            .yo-scroll {
                margin: 44px 0 55px;
            }
        }
    }
</style>
